package com.gin.kotlin.no_gay_alliance.activity.pm

import android.content.Intent
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.InputMethodUtils
import com.gin.kotlin.no_gay_alliance.databinding.ActivityPrivateMessagePostBinding
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import retrofit2.Call
import retrofit2.Response

class PrivateMessagePostActivity : AbstractBindingActivity<ActivityPrivateMessagePostBinding>() ,NgaCallback<List<String>> {
    private var messageId: Long?=null

    override fun onCreate() {
        val b = this.b?:return
        b.etContent.requestFocus()
        b.etContent.selectAll()
        uiDelayed(200){ InputMethodUtils.show(b.etContent,this)}
        Log.i(TAG, "回复私信: $messageId")
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId==R.id.mi_send_post){
            val b = this.b?:return false
            Log.i(TAG, "发送私信回复: $messageId")

            App.INSTANCE.ngaApi.messageApi.reply(messageId!!,b.etTitle.text.toString(),b.etContent.text.toString()).enqueue(this)

        }
        return super.onOptionsItemSelected(item)
    }

    override fun onSuccess(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, data: List<String>?) {
        data?.get(0)?.also { toast(it) }
        // 成功，关闭活动
        setResult(200)
        finish()
    }

    override fun onError(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, errorMessage: String) {
        toastLong(errorMessage)
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.post,menu)
        menu.findItem(R.id.mi_setting)?.isVisible = false
    }

    override fun handleIntent(intent: Intent) {
        this.messageId = intent.getLongExtra("id", 0L)
    }
}